package com.tedu.egspringmvc.controller;

import com.tedu.egspringmvc.mapper.AddressMapper;
import com.tedu.egspringmvc.pojo.dto.AddressDTO;
import com.tedu.egspringmvc.pojo.entity.Address;
import com.tedu.egspringmvc.pojo.vo.AddressVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "地址管理")
@Slf4j
public class AddressController {
    @Autowired
    private AddressMapper addressMapper;

    @PostMapping("/address/insert")
    @ApiOperation(value = "添加地址")
    public String insertAddress(AddressDTO addressDTO) {
        log.debug("addressDTO:" + addressDTO);
        Address address = new Address();
        BeanUtils.copyProperties(addressDTO, address);
        int num = addressMapper.insertAddress(address);
        return num > 0 ? "插入成功" : "插入失败";
    }

    @DeleteMapping("/address/delete/{tag}")
    @ApiOperation(value = "删除地址")
    @ApiImplicitParam(name = "tag", value = "地址标签", required = true, dataType = "String", example = "城市")
    public String deleteAddress(@PathVariable String tag) {
        log.debug("tag:" + tag);
        int num = addressMapper.deleteAddressByTag(tag);
        return num > 0 ? "删除成功" : "删除失败";
    }

    @PutMapping("/address/update")
    @ApiOperation(value = "修改地址")
    public String updateAddress(AddressDTO addressDTO) {
        log.debug("addressDTO:" + addressDTO);
        Address address = new Address();
        BeanUtils.copyProperties(addressDTO, address);
        int num = addressMapper.updateAddress(address);
        return num > 0 ? "修改成功" : "修改失败";
    }

    @GetMapping("/address/find")
    @ApiOperation(value = "查询所有地址信息")
    public List<AddressVO> findAll() {
        return addressMapper.findAll();
    }
}
